package net.shao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.shao.R;
import net.shao.model.ChatListModel;

import java.util.List;

/**
 * Created by shao on 2016/8/19.
 */
public class ChatListAdapter extends BaseAdapter {

    private List<ChatListModel> mChatList;
    private Context context;

    public ChatListAdapter(Context context, List<ChatListModel> mChatList) {
        this.mChatList = mChatList;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (mChatList.isEmpty()) {
            return 0;
        } else {
            return mChatList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (position == 0) {
            return 0;
        } else
            return mChatList.get(position);
    }

    @Override
    public long getItemId(int position) {
        if (position == 0) {
            return 0;
        } else
            return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_chat_list, null);
            viewHolder.img_headId = (ImageView) convertView.findViewById(R.id.img_headId);
            viewHolder.mChatTitle = (TextView) convertView.findViewById(R.id.tv_title_chat_list_item);
            viewHolder.mChatText = (TextView) convertView.findViewById(R.id.tv_text_chat_list_item);
           viewHolder.mChatTime = (TextView) convertView.findViewById(R.id.tv_time_chat_list_item);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ChatListModel model = mChatList.get(position);

        if (model.getChatType() == ChatListModel.CHAT_LIST_TYPE_FRIEND) {

        } else if (model.getChatType() == ChatListModel.CHAT_LIST_TYPE_GROUP) {

        }

        viewHolder.mChatTitle.setText(model.getChatTitle());
        viewHolder.mChatText.setText(model.getChatText());
        viewHolder.img_headId.setImageResource(model.getImgId());
        viewHolder.mChatTime.setText(model.getChatTime());
        return convertView;
    }

    static class ViewHolder {
        TextView mChatTitle;
        TextView mChatText;
        ImageView img_headId;
        TextView mChatTime;
    }

}
